package core;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.Image;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.Barcode39;
import com.itextpdf.text.pdf.BarcodeCodabar;
import com.itextpdf.text.pdf.BarcodeDatamatrix;
import com.itextpdf.text.pdf.BarcodeEAN;
import com.itextpdf.text.pdf.Barcode;
import com.itextpdf.text.pdf.BarcodePDF417;
import com.itextpdf.text.pdf.BarcodeQRCode;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
/**
 * This Class will be responsible for creating the PDF after items
 * have been added in the ItemBatchView.
 *
 */
public class ItemBarcodeGenerator 
{
	/**
	 * This method will take the items that were added and create the
	 * PDF with the item barcodes for printing.
	 * @param items list of items to create barcodes for
	 * @return true if it was successful, false otherwise.
	 * @throws DocumentException 
	 * @throws FileNotFoundException 
	 */
	public static String generateBarcodePDF(List<Item> items) throws 
								FileNotFoundException, DocumentException
	{
		String outputFile = "labels/labels-";//test.pdf";
		outputFile += new Date().getTime() + ".pdf";
		
		Document document = new Document();
		PdfWriter writer = PdfWriter.getInstance(document,  new FileOutputStream(outputFile));
		document.setMargins(18f, 12f, 25f, 25f);
		document.setMarginMirroringTopBottom(true);
		document.open();
		
		
		Font font = FontFactory.getFont(FontFactory.TIMES);
		font.setSize(7);
		
		PdfPTable allTable = new PdfPTable(6);
		allTable.setWidthPercentage(100);
		allTable.setSpacingBefore(200);
		
		for (int i = 0; i < items.size(); i++)
		{
			Item currItem = items.get(i);
			
			PdfPTable table = new PdfPTable(1);
			PdfPCell product;
			PdfPCell dates;
			PdfPCell bcImg;
			
			product = new PdfPCell(new Phrase(currItem.getProduct().getDescription(), font));
			product.setPaddingBottom(0);
			product.setPaddingTop(10);
			product.setBorder(PdfPCell.NO_BORDER);
			
			
			String barcodeDateString = (currItem.getEntryDate().get(GregorianCalendar.MONTH) + 1)
					+ "\\" + currItem.getEntryDate().get(GregorianCalendar.DAY_OF_MONTH) + "\\" +
					currItem.getEntryDate().get(GregorianCalendar.YEAR);
			
			//if (!currItem.getEntryDate().equals(currItem.getExpirationDate()))
			if (currItem.getExpirationDate() != null)
			{
				barcodeDateString += " exp " + 
					(currItem.getExpirationDate().get(GregorianCalendar.MONTH) + 1) + "\\" + 
					currItem.getExpirationDate().get(GregorianCalendar.DAY_OF_MONTH) + "\\" +
					currItem.getExpirationDate().get(GregorianCalendar.YEAR);
			}
			
			
			dates = new PdfPCell(new Phrase(barcodeDateString, font));
			dates.setPaddingBottom(0);
			dates.setPaddingTop(0);
			dates.setBorder(PdfPCell.NO_BORDER);
						
			BarcodeEAN barcode = new BarcodeEAN();
			barcode.setCodeType(Barcode.UPCA);
			barcode.setCode(currItem.getBarcodeString());
			barcode.setBarHeight(20);
			Image codeImg = barcode.createImageWithBarcode(writer.getDirectContent(), null, null);
			codeImg.scalePercent(95);
			//document.add(codeImg);
			
			bcImg = new PdfPCell(codeImg);
			bcImg.setPaddingTop(2);
			bcImg.setPaddingBottom(10);
			bcImg.setPaddingLeft(2);
			bcImg.setBorder(PdfPCell.NO_BORDER);
			
			table.addCell(product);
			table.addCell(dates);
			table.addCell(bcImg);
			
			//document.add(table);
			PdfPCell newAllTableCell = new PdfPCell(table);
			newAllTableCell.setBorder(PdfPCell.NO_BORDER);
			
			/*if (i == (items.size() - 1))
			{
				int remaining = (i+1) % 6;
				int boxesToAdd = 6-remaining;
				if (boxesToAdd != 6)
				{
					newAllTableCell.setRowspan(i+boxesToAdd+1);
				}
			}*/
			
			allTable.addCell(newAllTableCell);
			
			
		}
		
		for(int i = 0; i < 6; i++)
		{
			PdfPCell newCell = new PdfPCell(new Phrase(" "));
			newCell.setBorder(PdfPCell.NO_BORDER);
			
			allTable.addCell(newCell);
		}
		
		document.add(allTable);
		
		document.close();
		
		return outputFile;
	}
}
